<!--组卷规则组件-->
<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search">
            <div class="search-item">
                <label>考试类型：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.paperType"
                        placeholder="请选择组卷类型"
                    >
                        <el-option
                            label="章节测试"
                            value="chapterText"
                        ></el-option>
                        <el-option
                            label="期末考试"
                            value="finalExam"
                        ></el-option>
                        <el-option
                            label="普通考试"
                            value="normalExam"
                        ></el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>规则类型：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.ruleType"
                        placeholder="请选择组卷类型"
                    >
                        <el-option label="随机组卷" value="random"></el-option>
                        <el-option label="抽题组卷" value="select"></el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>规则名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="规则名称"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>学校：</label>
                <el-cascader
                    ref="schoolRef"
                    :key="key"
                    v-model="queryData.schoolCode"
                    :props="schoolProps"
                    :show-all-levels="false"
                    filterable
                    clearable
                    @change="handleSchool"
                ></el-cascader>
            </div>
            <div class="search-item">
                <label>专业：</label>
                <div class="table-query-input">
                    <el-cascader
                        ref="departmentRef"
                        v-model="queryData.departmentId"
                        v-loading="departmentLoading"
                        :options="departmentInfo"
                        placeholder="学科"
                        :show-all-levels="false"
                        clearable
                        filterable
                        :props="{
                            value: 'id',
                            label: 'name',
                            emitPath: false,
                            checkStrictly: true
                        }"
                    >
                    </el-cascader>
                </div>
            </div>

            <!--            <div v-if="queryData.paperType!=='normalExam'" class="search-item" >-->
            <!--                <label>课程：</label>-->
            <!--                <div class="table-query-input">-->
            <!--                    <el-cascader-->
            <!--                        ref="courseRef"-->
            <!--                        v-model="queryData.courseId"-->
            <!--                        v-loading="courseFlag"-->
            <!--                        :options="courseInfo"-->
            <!--                        placeholder="请选择课程"-->
            <!--                        :show-all-levels="false"-->
            <!--                        clearable-->
            <!--                        filterable-->
            <!--                        :props="{-->
            <!--                                value: 'id',-->
            <!--                                label: 'courseName',-->
            <!--                                emitPath: false,-->
            <!--                                checkStrictly: true-->
            <!--                            }"-->

            <!--                    >-->
            <!--                    </el-cascader>-->
            <!--                </div>-->
            <!--            </div>-->
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                v-if="!isShow"
                type="primary"
                plain
                style="margin-bottom: 2em"
                @click="sendToparent"
            >确定选择
            </el-button>
            <el-button
                v-if="isShow"
                type="danger"
                plain
                style="margin-bottom: 2em"
                :disabled="ids.length === 0"
                @click="
                    tableDelete(
                        '/exam/rule/deleted-list',
                        {
                            ids: ids
                        },
                        true
                    )
                "
            >批量删除
            </el-button>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                @row-click="chooseone"
                @current-change="handleSelectionChange"
                @selection-change="handleSelectionChange2"
            >
                <el-table-column v-if="!isShow" width="55" label="选择">
                    <template slot-scope="scope">
                        <el-radio v-model="radio" :label="scope.row.id">
                            <span class="el-radio__label"></span>
                        </el-radio>
                    </template>
                </el-table-column>
                <el-table-column
                    v-if="isShow"
                    type="selection"
                    min-width="55"
                    align="center"
                    :resizable="false"
                ></el-table-column>
                <el-table-column
                    prop="name"
                    label="组卷名称"
                    width="160"
                ></el-table-column>
                <el-table-column prop="paperType" label="考试分类" width="160">
                    <template slot-scope="scope">
                        {{ status[scope.row.paperType] }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="departmentName"
                    label="学科"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="classificationName"
                    label="科目"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="courseName"
                    label="所属课程"
                    width="160"
                ></el-table-column>
                <el-table-column prop="ruleType" label="组卷方式" width="120">
                    <template slot-scope="scope">
                        {{ ruleType[scope.row.ruleType] }}
                    </template>
                </el-table-column>
                <el-table-column prop="sourceType" label="题库" width="120">
                    <template slot-scope="scope">
                        {{ ruleSourceType[scope.row.sourceType] }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="totalScore"
                    label="总分"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="passScore"
                    label="及格分"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="createdByName"
                    label="最后操作人"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="createdTime"
                    label="创建时间"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="remark"
                    label="描述"
                    min-width="160"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    v-if="isShow"
                    label="操作"
                    align="center"
                    fixed="right"
                    min-width="300"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="toEditPg(scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                            type="warning"
                            icon="el-icon-delete"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/exam/rule/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true,
                                    search
                                )
                            "
                        >删除
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            type="primary"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="toPaperQuestion(scope.row)"
                        >查看试卷
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            v-if="paperQuestionVisible"
            title="查看试卷"
            :visible.sync="paperQuestionVisible"
            width="80%"
        >
            <paperQuestionComponent
                :data-form="paperData"
            ></paperQuestionComponent>
            <span slot="footer" class="dialog-footer">
                <el-button @click="paperQuestionVisible = false"
                >关 闭</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import {beanCopy, treeList2Tree} from "@/utils";
import {fetchCourseList} from "@/api/course";
import {
    fetchDepartmentList,
    fetchSchoolChildren,
    fetchSchoolFirst
} from "@/api/user";
import {fetchPaperByRuleId} from "@/api/exam";
import paperQuestionComponent from "@/views/models/exam/component/paperQuestionComponent";

export default {
    name: "GroupPaperRuleComponent",
    components: {paperQuestionComponent},
    mixins: [TablePage],
    props: {
        initParam: {
            type: Object,
            default() {
                return {};
            }
        },
        isShow: {
            type: Boolean,
            default() {
                return true;
            }
        },
        fromExam: {
            type: Boolean,
            default() {
                return false;
            }
        }
    },
    data() {
        return {
            queryData: {
                ...this.initParam
            },
            initAddForm: {
                id: null,
                name: null,
                remark: null,
                actionCodes: []
            },
            paperData: {},
            schoolProps: {
                lazy: true,
                checkStrictly: true,
                emitPath: false,
                lazyLoad: this.getSchool
            },
            ids: [],
            courseQueryData: {},
            key: 0,
            departmentLoading: false,
            paperQuestionVisible: false,
            departmentInfo: [],
            courseFlag: false,
            courseInfo: [],
            radio: null,
            currentSelectItem: {},
            actions: null,
            actionSearch: null,
            src:
                "https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg",
            contentDialogVisible: false,
            classificationInfo: [],
            content: [],
            initContent: {
                firstType: "",
                secondType: "",
                easyNum: "",
                normalNum: "",
                diffcultNum: "",
                everyScore: ""
            },
            options: [
                {
                    value: "singleChoice",
                    label: "单选题"
                },
                {
                    value: "multipleChoice",
                    label: "多选题"
                },
                {
                    value: "judgment",
                    label: "判断题"
                },
                {
                    value: "fill",
                    label: "填空题"
                },
                {
                    value: "text",
                    label: "简答题"
                },
                {
                    value: "combination",
                    label: "组合题"
                }
            ],
            secondTypeInfo: []
        };
    },
    computed: {
        ...mapState({
            status: state => state.status.paperType,
            ruleType: state => state.status.ruleType,
            questionFirstType: state => state.status.questionFirstType,
            ruleSourceType: state => state.status.ruleSourceType
        })
    },
    watch: {},
    mounted() {
    },
    created() {
    },
    methods: {

        search() {
            this.loadTable("/exam/rule/page", this.queryData);
        },
        toEditPg(data) {
            data.addActioon = false;
            this.$router.push({
                name: "GroupAddRule",
                query: {paperType: this.queryData.paperType, ruleId: data.id}
            });
        },
        handleSelectionChange(row) {
            this.currentSelectItem = row;
        },
        handleSelectionChange2(val) {
            this.ids = [];
            const length = val.length;
            for (let n = 0; n < length; n++) {
                this.ids.push(val[n].id);
            }
        },
        chooseone(row) {
            this.radio = row.id;
        },
        sendToparent() {
            this.$emit("ruleSelectEvent", this.currentSelectItem);
            this.radio = null;
            this.currentSelectItem = {};
        },
        getSchool(node, resolve) {
            const {level} = node;
            let disable = false;
            if (!node.data) {
                fetchSchoolFirst().then(res => {
                    if (res !== null) {
                        const nodes = Array.from(res).map(item => {
                            disable =
                                (this.currentSchoolId !== null &&
                                    item.id === this.currentSchoolId) ||
                                "vip-langrun" === item.code;
                            return {
                                value: item.code,
                                label: `${item.name}`,
                                leaf:
                                    level >= (item.children.length > 0 ? 1 : 0),
                                code: item.code,
                                disabled: disable
                            };
                        });
                        resolve(nodes);
                    }
                });
            } else {
                fetchSchoolChildren({id: node.data.value}).then(res => {
                    const nodes = Array.from(res).map(item => {
                        disable =
                            (this.currentSchoolId !== null &&
                                item.id === this.currentSchoolId) ||
                            this.currentSchoolId === item.parentId ||
                            "vip-langrun" === item.code;
                        return {
                            value: item.code,
                            label: `${item.name}`,
                            leaf:
                                level >=
                                (item.children.length > 0 ? level + 1 : 0),
                            code: item.code,
                            disabled: disable
                        };
                    });
                    resolve(nodes);
                });
            }
        },
        //根据学校查询专业
        handleSchool(data) {
            let checkedNodes = this.$refs.schoolRef.getCheckedNodes();

            this.queryData.schoolCode = checkedNodes[0].data.code;
            this.departmentLoading = true;
            fetchDepartmentList(
                {schoolCode: this.queryData.schoolCode, type: "xklb"},
                "查询专业信息异常"
            ).then(res => {
                this.departmentInfo = treeList2Tree(res);
                this.departmentLoading = false;
            });
        },
        handleDepartment(value) {
            this.queryData.courseId = null;
            this.queryData.chapterId = null;
            this.courseFlag = true;
            this.courseQueryData.schoolMajorId = value;
            fetchCourseList(this.courseQueryData, "查询课程信息异常").then(
                res => {
                    this.courseInfo = treeList2Tree(res);
                    this.courseFlag = false;
                }
            );
        },
        toPaperQuestion(data) {
            //查询关联试卷
            fetchPaperByRuleId({id: data.id}, "查询关联试卷异常").then(
                res => {
                    this.paperData = res;
                    this.paperQuestionVisible = true;
                }
            );
        }
    }
};
</script>

<style lang="scss" scoped>
.container {
    background: #fff;
    border-radius: 0.3em 0.3em 0 0;
    padding-bottom: 1em;
    padding-top: 1em;
    padding-left: 1em;
    margin-top: 16px;

    .container-content {
        padding-top: 12px;

        p {
            color: #909399;
            font-size: 15px;
            padding-top: 12px;
        }
    }

    .container-right {
        margin-left: 12px;
        margin-top: 2em;

        .r-img {
            max-width: 240px;
            max-height: 160px;
        }

        .r-btn {
            margin-top: 22px;
        }
    }
}

.table-search-box {
    padding-left: 17px;
    font-size: 16px;
}
</style>
